package com.fbs.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fbs.entity.Confessionwall;
import com.fbs.mapper.ConfessionwallMapper;
import com.fbs.service.ConfessionwallService;
import com.fbs.vo.Result;
import com.fbs.vo.WallVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 13745
* @description 针对表【confessionwall】的数据库操作Service实现
* @createDate 2023-04-23 18:52:42
*/
@Service
public class ConfessionwallServiceImpl extends ServiceImpl<ConfessionwallMapper, Confessionwall>
implements ConfessionwallService {
    @Autowired
    private ConfessionwallMapper confessionwallMapper;

    @Override
    public Result findAll() {
        QueryWrapper<Confessionwall> wrapper = new QueryWrapper<>();
        wrapper.eq("status",0);
        wrapper.select().orderByDesc("publishdate");
        List<Confessionwall> confessionwalls = confessionwallMapper.selectList(wrapper);
        List<WallVo> wallVos=new ArrayList<>();
        for (Confessionwall confessionwall : confessionwalls) {
            String[] split = confessionwall.getImg().split(",");
            wallVos.add(new WallVo(confessionwall.getId(),confessionwall.getPublishdate(),confessionwall.getDetails(),split,confessionwall.getRemark()));
        }
        return new Result(2000,"查询成功",wallVos);
    }

    @Override
    public Result addwall(WallVo wallVo) {
        String collect = Arrays.asList(wallVo.getImg()).stream().collect(Collectors.joining(","));

        int insert = confessionwallMapper.insert(new Confessionwall(null,new Date(),wallVo.getDetails(),collect,wallVo.getRemark(),0));
        if (insert!=0){
            return new Result(2000,"发布成功");
        }
        return new Result(5000,"发布失败");
    }
}
